﻿using System;
using System.Runtime.InteropServices;

namespace MatlabBridge.NativeMethods
{
    internal static class Utils
    {
        public static mxClassID ClassIDFromType(Type t)
        {
            if (t == typeof(ulong))
                return mxClassID.mxUINT64_CLASS;
            else if (t == typeof(uint))
                return mxClassID.mxUINT32_CLASS;
            else if (t == typeof(ushort))
                return mxClassID.mxUINT16_CLASS;
            else if (t == typeof(byte))
                return mxClassID.mxUINT8_CLASS;
            else if (t == typeof(long))
                return mxClassID.mxINT64_CLASS;
            else if (t == typeof(short))
                return mxClassID.mxINT16_CLASS;
            else if (t == typeof(int))
                return mxClassID.mxINT32_CLASS;
            else if (t == typeof(sbyte))
                return mxClassID.mxINT8_CLASS;
            else if (t == typeof(double))
                return mxClassID.mxDOUBLE_CLASS;
            else if (t == typeof(float))
                return mxClassID.mxSINGLE_CLASS;
            else if (t == typeof(bool))
                return mxClassID.mxLOGICAL_CLASS;
            else if (t == typeof(char))
                return mxClassID.mxCHAR_CLASS;
            else
                return mxClassID.mxUNKNOWN_CLASS;
        }
        public static Type TypeFromClassID(mxClassID classid)
        {
            switch (classid)
            {
                case mxClassID.mxUINT64_CLASS:
                    return typeof(ulong);
                case mxClassID.mxUINT32_CLASS:
                    return typeof(uint);
                case mxClassID.mxUINT16_CLASS:
                    return typeof(ushort);
                case mxClassID.mxUINT8_CLASS:
                    return typeof(byte);
                case mxClassID.mxINT64_CLASS:
                    return typeof(long);
                case mxClassID.mxINT32_CLASS:
                    return typeof(int);
                case mxClassID.mxINT16_CLASS:
                    return typeof(short);
                case mxClassID.mxINT8_CLASS:
                    return typeof(sbyte);
                case mxClassID.mxDOUBLE_CLASS:
                    return typeof(double);
                case mxClassID.mxSINGLE_CLASS:
                    return typeof(float);
                case mxClassID.mxLOGICAL_CLASS:
                    return typeof(bool);
                case mxClassID.mxCHAR_CLASS:
                    return typeof(char);
                default:
                    throw new ArgumentOutOfRangeException("classid", "Class ID could not be expressed as a CLR type.");
            }
        }
    }
}
